<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>留言列表</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__ADMIN_STATIC__/css/oksub.css">
  <script type="text/javascript" src="__STATIC__/lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
  <!--模糊搜索区域-->
  <div class="layui-row">
    <form class="layui-form ok-search-form">
      <div class="layui-form-item">
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="start_time">
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="end_time">
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <select name="visited">
              <option value="">选择状态</option>
              <option value="1">未查看</option>
              <option value="2">已查看</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <button class="layui-btn" lay-submit="" lay-filter="search">
              <i class="layui-icon">&#xe615;</i>
            </button>
          </div>
        </div>
      </div>
    </form>
  </div>
  <!--数据表格-->
  <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="__STATIC__/lib/layui/layui.js"></script>
<script>
    layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
        let table = layui.table;
        let form = layui.form;
        let util = layui.util;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;
        okLoading.close();
        util.fixbar({});

        laydate.render({elem: "#startTime", type: "datetime"});
        laydate.render({elem: "#endTime", type: "datetime"});

        let articleTable = table.render({
            elem: "#tableId",
            url: "{:url('message/list')}",
            limit: 20,
            page: true,
            even: true,
            toolbar: "#toolbarTpl",
            size: "sm",
            cols: [[
                {type: "checkbox", fixed: "left"},
                {field: "id", title: "ID", width: 100, sort: true},
                {field: "username", title: "用户名", width: 150},
                {field: "title", title: "标题", width: 170},
                {field: "name", title: "联系人", width: 150},
                {field: "email", title: "联系邮箱", width: 150},
                {field: "visited", title: "状态", width: 120, templet: "#visitedTpl"},
                {field: "create_time", title: "创建时间", width: 150},
                {title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count)
            }
        });

        form.on("submit(search)", function (data) {
            articleTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            if(obj.event === "batchDel"){
                batchDel();
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.id);
                    break;
                case "del":
                    del(data.id);
                    break;
                case "read":
                    read(data.id);
                    break;
            }
        });


        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                var idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("{:url('message/batchDel')}", "delete", {ids: idsStr}, true).done(function (response) {
                        console.log(response);
                        okUtils.tableSuccessMsg(response.msg);
                    }).fail(function (error) {
                        console.log(error)
                    });
                }
            });
        }

        function edit(id) {
            okLayer.open("编辑留言", "{:url('message/edit')}?id=" + id, "90%", "90%", null, function () {
                articleTable.reload();
            });
        }
        function read(id) {
            okLayer.open("查看留言", "{:url('message/read')}?id=" + id, "90%", "90%", null, function () {
                articleTable.reload();
            });
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("{:url('message/delete')}", "delete", {id: id}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    })
</script>
<!-- 类型模板 -->
<script type="text/html" id="visitedTpl">
  {{#  if(d.visited == 1){ }}
  <span class="layui-badge">未查看</span>
  {{#  } else { }}
  <span class="layui-badge layui-bg-blue">已查看</span>
  {{#  } }}
</script>

<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
  <a href="javascript:" title="查看" lay-event="read"><i class="layui-icon">&#xe660;</i></a>
  <a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
  <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>


<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
  </div>
</script>



</body>
</html>
